/* Turska Framework/UI library (Turs2)
 * Copyright 2003-2010 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/turska/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: T2Image9Tile.cpp 28 2012-07-28 17:03:06Z trzntaat $
 * $Revision: 28 $
 */

#include "Turs2.h"

#include "T2Image9Tile.h"


namespace turska
{


void Image9Tile::draw(TREAL x, TREAL y)
{
    draw(mDrawMode, mFlags, x, y, mTargetWidth, mTargetHeight, mColor);
}


void Image9Tile::draw(IMAGE_DRAW_MODE drawMode, UI32 flags,
                      TREAL x, TREAL y, TREAL width, TREAL height, const Color &color)
{
    // adjust x,y to be top-left
    if (flags & IF_ALIGN_RIGHT)
        x -= width;
    else if (flags & IF_ALIGN_HORIZ_CENTER)
        x -= width / 2;
    if (flags & IF_ALIGN_BOTTOM)
        y -= height;
    else if (flags & IF_ALIGN_VERT_CENTER)
        y -= height / 2;
    x = (TREAL)(I32)x;
    y = (TREAL)(I32)y;

    TREAL newWidth = width, newHeight = height;
    if (!mBorderOutside)
    {
        newWidth = width - (TREAL)(mLeftWidth + mRightWidth);
        newHeight = height - (TREAL)(mTopHeight + mBottomHeight);
        if (newWidth < 0 || newHeight < 0)
        {
            // draw just the center with the original width & height since borders don't fit..
            drawTooSmallInside(drawMode, flags, x, y, width, height, color);
            return;
        }
        x += (TREAL)mLeftWidth;
        y += (TREAL)mTopHeight;
    }

    drawTopLeftOutside(drawMode, flags, x, y, newWidth, newHeight, color);
}


void Image9Tile::drawTopLeftOutside(IMAGE_DRAW_MODE drawMode, UI32 flags,
                                    TREAL x, TREAL y, TREAL width, TREAL height, const Color &color)
{
    // source tile sizes
    const TREAL widths[3] = { (TREAL)mLeftWidth, (TREAL)mCenterWidth, (TREAL)mRightWidth };
    const TREAL heights[3] = { (TREAL)mTopHeight, (TREAL)mCenterHeight, (TREAL)mBottomHeight };
    // target sizes
    const TREAL targetWidths[3] = { widths[0], width, widths[2] };
    const TREAL targetHeights[3] = { heights[0], height, heights[2] };
    Rectangle srcRect(0, 0, 0, 0);
    // clear flags which aren't supported here or are handled already
    flags &= IF_COLOR | IF_ALPHA;
    TREAL py = y - (TREAL)mTopHeight;
    I32 tileEnabledMaskBitIndex = 0;
    for (I32 ty = 0; ty < 3; ++ty)
    {
        srcRect.x = 0;
        srcRect.height = heights[ty];
        TREAL px = x - (TREAL)mLeftWidth;
        for (I32 tx = 0; tx < 3; ++tx, ++tileEnabledMaskBitIndex)
        {
            if ((mTileEnabledMask & (1 << tileEnabledMaskBitIndex)) != 0)
            {
                // tile is enabled, draw it
                srcRect.width = widths[tx];
                mImage->setTargetSize(targetWidths[tx], targetHeights[ty]);
                mImage->draw(drawMode, flags, px, py, color, &srcRect);
            }
            srcRect.x += widths[tx];
            px += targetWidths[tx];
        }
        srcRect.y += heights[ty];
        py += targetHeights[ty];
    }
    mImage->resetTargetSize();
}


void Image9Tile::drawTooSmallInside(IMAGE_DRAW_MODE drawMode, UI32 flags,
                                    TREAL x, TREAL y, TREAL width, TREAL height, const Color &color)
{
    Rectangle srcRect((TREAL)mLeftWidth, (TREAL)mTopHeight, (TREAL)mCenterWidth, (TREAL)mCenterHeight);
    mImage->setTargetSize(width, height);
    mImage->draw(drawMode, flags, x, y, color, &srcRect);
}


} // namespace turska
